package com.bdqn.controller;

import com.bdqn.LayUI.Layui;
import com.bdqn.Service.Orders.OrdersService;
import com.bdqn.Service.Supplier.SupplierService;
import com.bdqn.entity.Employee;
import com.bdqn.entity.Material;
import com.bdqn.entity.Orders;
import com.bdqn.entity.Supplier;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/orders")
@ResponseBody
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    @Autowired
    SupplierService supplierService;
    @ResponseBody
    @RequestMapping("/")
    public Layui<Orders> SelectMat(@Param("limit") Integer limit,/*页大小*/
                                     @Param("page") Integer page/*页码*/,Orders ord,@RequestParam(value = "px",required = false)String px){
        int page1 = (page - 1) * limit;
        List<Orders> orders=ordersService.findAll(ord,limit,page1,px);
        int count=ordersService.count(ord);
        //新建layui分页需要的返回数据类型。
        Layui<Orders> lists = new Layui<Orders>();
        lists.setCode(0);
        lists.setCount(count);
        lists.setData(orders);
        return lists;
    }
    @ResponseBody
    @RequestMapping("/find1")
    public Layui<Orders> find1(@Param("limit") Integer limit,/*页大小*/
                                   @Param("page") Integer page/*页码*/,Orders ord,@RequestParam(value = "px",required = false)String px){
        int page1 = (page - 1) * limit;
        List<Orders> orders=ordersService.findAll1(ord,limit,page1,px);
        int count=ordersService.count1(ord);
        //新建layui分页需要的返回数据类型。
        Layui<Orders> lists = new Layui<Orders>();
        lists.setCode(0);
        lists.setCount(count);
        lists.setData(orders);
        return lists;
    }
    @ResponseBody
    @RequestMapping("/SelById")
    public Orders SelById(Orders ord){
        Orders order=ordersService.findById(ord);
        return order;
    }
    @ResponseBody
    @RequestMapping("/Que")
    public boolean Que(@Param("id") int id){
        return ordersService.Que(id);
    }
    @ResponseBody
    @RequestMapping("/Del")
    public boolean Del(@Param("id") int id){
        return ordersService.Del(id);
    }
    @ResponseBody
    @GetMapping("/DelWei")
    public boolean DelWei(){
        return ordersService.DelectWei();
    }
    @ResponseBody
    @PostMapping("Update")
    public boolean Update(Orders ord){
        Subject subject= SecurityUtils.getSubject();
        Session session = subject.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
        ord.setAuthorId(employee.getEmpNum());
        ord.setAuthor(employee.getEmpName());
        ord.setPhone(employee.getPhone());
        ord.setFax(employee.getFax());
        ord.setEmail(employee.getEmail());
        return ordersService.Update(ord);
    }
    @ResponseBody
    @RequestMapping("/Sel")
    public Layui<Orders> Sel(){
        Subject subject= SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getSession().getAttribute("employee");//员工
        Supplier supplier = supplierService.selectSupplierById(employee.getId());
        List<Orders> orders = ordersService.SelByG(supplier.getId());
        Layui<Orders> lists = new Layui<Orders>();
        lists.setCode(0);
        lists.setData(orders);
        return lists;
    }
}
